package com.pan.framework.core;

import com.pan.framework.core.aop.MyAopProxyFactory;
import com.pan.framework.core.container.MyContainer;
import com.pan.framework.core.factory.MyBeanInit;
import com.pan.framework.core.factory.MyFactory;
import com.pan.framework.core.factory.scan.MyScan;
import com.pan.framework.entity.BeanInfo;
import com.pan.framework.tomcat.MyServletFactory;
import com.pan.framework.tomcat.MyTomcat;
import com.pan.framework.tomcat.entity.ServletInfo;
import org.apache.catalina.LifecycleException;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.List;
import java.util.stream.Collectors;

public class MyFramework {



    private MyContainer container;
    private MyScan scan;
    private MyFactory factory;
    private MyBeanInit beanInit;

    private MyTomcat myTomcat;
    private MyServletFactory servletFactory;
    private MyAopProxyFactory proxyFactory;
    private final static MyFramework myFramework =  new MyFramework();

    private MyFramework(){
        container = new MyContainer();

    }

    private void init(String packageName){
        //打印log  src/main/resources
        File file = new File("./src/main/resources/MyFrameLog.mylog");
        BufferedReader reader;
        try {
            reader = new BufferedReader(new FileReader(file));
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        }
        reader.lines().forEach(System.out::println);
        scan = new MyScan(packageName);
        System.out.println("扫描项目成功");
        factory = new MyFactory(container,scan);
        factory.init();
        System.out.println("创建对象成功");


        proxyFactory = new MyAopProxyFactory(container,scan);
        List<BeanInfo> aspectList = container.getAspect();
        try {
            proxyFactory.aspectHandler(aspectList.stream().map(BeanInfo::getName).collect(Collectors.toList()));
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        System.out.println("切面代理成功");


        beanInit = new MyBeanInit(container,scan);
        beanInit.init();
        System.out.println("注入对象成功");

        myTomcat = new MyTomcat();
        servletFactory = new MyServletFactory(scan.getMyLoader());
        List<BeanInfo> controllerBeans = container.getController();
        List<Object> controllers = controllerBeans.stream()
                .map(BeanInfo::getBean).collect(Collectors.toList());
        List<ServletInfo> servletInfoList = servletFactory.getServletInfoList(controllers);
        System.out.println("创建servlet成功");

        myTomcat.addServer(servletInfoList);
        System.out.println("映射servlet成功");





        try {
            System.out.println("tomcat 启动");
            myTomcat.run();
        } catch (LifecycleException e) {
            throw new RuntimeException(e);
        }
    }

    public static void run(Class appClass){
        myFramework.init(appClass.getPackage().getName());

    }


}
